﻿using System;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKDimensionModel.DetailIndex
{
	[Transaction(TransactionMode.Manual)]
	internal class TestCmd : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Document document2 = commandData.Application.Application.OpenDocumentFile("C:\\Hongye\\YJKArch2016\\FamilyLib\\Family\\2016\\DetailIndex\\HY-IndexMark.rfa");
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document2);
			View view = filteredElementCollector.OfClass(typeof(View)).First<Element>() as View;
			XYZ xyz = new XYZ(74.2873353545275, 1.21691347297134E-21, 8.48054894715269E-16);
			XYZ xyz2 = new XYZ(74.2873353545275, 1.06006863470958E-15, 0.656167979002625);
			XYZ xyz3 = new XYZ(74.2873353545275, 0.262467191601051, 0.656167979002624);
			XYZ xyz4 = new XYZ(74.2873353545275, 0.26246719160105, 0.0);
			ViewSection viewSection = document.GetElement(new ElementId(8176)) as ViewSection;
			Transform transform = Transform.Identity;
			transform.Origin = viewSection.Origin;
			transform.BasisX = viewSection.RightDirection;
			transform.BasisY = viewSection.UpDirection;
			transform.BasisZ = viewSection.ViewDirection;
			transform = transform.Inverse;
			XYZ xyz5 = transform.OfPoint(viewSection.Origin);
			XYZ xyz6 = view.Origin - xyz5;
			Transform transform2 = Transform.CreateTranslation(xyz6);
			transform2 = transform2.ScaleBasis(0.01);
			transform = transform2 * transform;
			xyz = transform.OfPoint(xyz);
			xyz2 = transform.OfPoint(xyz2);
			xyz3 = transform.OfPoint(xyz3);
			xyz4 = transform.OfPoint(xyz4);
			Curve curve = Line.CreateBound(xyz, xyz2);
			Curve curve2 = Line.CreateBound(xyz2, xyz3);
			Curve curve3 = Line.CreateBound(xyz3, xyz4);
			Curve curve4 = Line.CreateBound(xyz4, xyz);
			Transaction transaction = new Transaction(document2, "123");
			transaction.Start();
			document2.FamilyCreate.NewDetailCurve(view, curve);
			document2.FamilyCreate.NewDetailCurve(view, curve2);
			document2.FamilyCreate.NewDetailCurve(view, curve3);
			document2.FamilyCreate.NewDetailCurve(view, curve4);
			transaction.Commit();
			document2.SaveAs("C:\\Users\\Administrator\\Desktop\\12321.rfa", new SaveAsOptions
			{
				OverwriteExistingFile = true
			});
			Transaction transaction2 = new Transaction(document, "1233");
			transaction2.Start();
			Family family = null;
			document.LoadFamily("C:\\Users\\Administrator\\Desktop\\12321.rfa", out family);
			FamilySymbol familySymbol = document.GetElement(family.GetFamilySymbolIds().First<ElementId>()) as FamilySymbol;
			familySymbol.Activate();
			document.Create.NewFamilyInstance(viewSection.Origin, familySymbol, viewSection);
			transaction2.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
